Refactor the map_marker partial.

Akinori MUSHA 10 anni fa
parent
commit
951648c94e

+ 41 - 0
app/assets/javascripts/map_marker.js.coffee

@@ -0,0 +1,41 @@
1
+window.map_marker = (map, options = {}) ->
2
+  pos = new google.maps.LatLng(options.lat, options.lng)
3
+
4
+  if options.radius > 0
5
+    new google.maps.Circle
6
+      map: map
7
+      strokeColor: '#FF0000'
8
+      strokeOpacity: 0.8
9
+      strokeWeight: 2
10
+      fillColor: '#FF0000'
11
+      fillOpacity: 0.35
12
+      center: pos
13
+      radius: options.radius
14
+  else
15
+    new google.maps.Marker
16
+      map: map
17
+      position: pos
18
+      title: 'Recorded Location'
19
+
20
+  if options.course
21
+    p1 = new LatLon(pos.lat(), pos.lng())
22
+    speed = if options.speed? then options.speed else 1
23
+    p2 = p1.destinationPoint(options.course, Math.max(0.2, speed) * 0.1)
24
+
25
+    lineCoordinates = [
26
+      pos
27
+      new google.maps.LatLng(p2.lat(), p2.lon())
28
+    ]
29
+
30
+    lineSymbol =
31
+      path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW
32
+
33
+    new google.maps.Polyline
34
+      map: map
35
+      path: lineCoordinates
36
+      icons: [
37
+        {
38
+          icon: lineSymbol
39
+          offset: '100%'
40
+        }
41
+      ]

+ 3 - 4
app/views/agents/agent_views/user_location_agent/_show.html.erb

@@ -14,11 +14,10 @@
14 14
     };
15 15
 
16 16
     var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
17
+    <% events.each do |event| %>
18
+      <%= render "shared/map_marker", map: 'map', location: event.location %>
19
+    <% end %>
17 20
   </script>
18
-
19
-  <% events.each do |event| %>
20
-    <%= render "shared/map_marker", event: event %>
21
-  <% end %>
22 21
 <% else %>
23 22
   <p>
24 23
     No events found.

+ 2 - 2
app/views/events/show.html.erb

@@ -36,9 +36,9 @@
36 36
           };
37 37
 
38 38
           var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
39
-        </script>
40 39
 
41
-        <%= render "shared/map_marker", event: @event %>
40
+          <%= render "shared/map_marker", map: 'map', location: @event.location %>
41
+        </script>
42 42
       <% end %>
43 43
 
44 44
       <br />

+ 13 - 61
app/views/shared/_map_marker.html.erb

@@ -1,61 +1,13 @@
1
-<script>
2
-  (function(map) {
3
-    <%
4
-       if event.payload[:horizontal_accuracy] && event.payload[:vertical_accuracy]
5
-         radius = (event.payload[:horizontal_accuracy].to_f + event.payload[:vertical_accuracy].to_f) / 2.0
6
-       elsif event.payload[:horizontal_accuracy]
7
-         radius = event.payload[:horizontal_accuracy].to_f
8
-       elsif event.payload[:vertical_accuracy]
9
-         radius = event.payload[:vertical_accuracy].to_f
10
-       elsif event.payload[:accuracy]
11
-         radius = event.payload[:accuracy].to_f
12
-       else
13
-         radius = 0
14
-       end
15
-    %>
16
-
17
-    var pos = new google.maps.LatLng(<%= event.lat %>, <%= event.lng %>);
18
-
19
-    <% if radius > 0 %>
20
-      var accuracyCircle = new google.maps.Circle({
21
-        strokeColor: '#FF0000',
22
-        strokeOpacity: 0.8,
23
-        strokeWeight: 2,
24
-        fillColor: '#FF0000',
25
-        fillOpacity: 0.35,
26
-        map: map,
27
-        center: pos,
28
-        radius: <%= radius %>
29
-      });
30
-    <% else %>
31
-      var marker = new google.maps.Marker({
32
-        position: pos,
33
-        map: map,
34
-        title: 'Recorded Location'
35
-      });
36
-    <% end %>
37
-
38
-
39
-    <% if event.payload[:course] && event.payload[:course].to_f > -1 %>
40
-      var p1 = new LatLon(pos.lat(), pos.lng());
41
-      var p2 = p1.destinationPoint(<%= event.payload[:course].to_f %>, <%= [0.2, (event.payload[:speed] || 1).to_f].max * 0.1 %>);
42
-
43
-      var lineCoordinates = [ pos, new google.maps.LatLng(p2.lat(), p2.lon()) ];
44
-
45
-      var lineSymbol = {
46
-        path:google.maps.SymbolPath.FORWARD_CLOSED_ARROW
47
-      };
48
-
49
-      var line = new google.maps.Polyline({
50
-        path: lineCoordinates,
51
-        icons: [
52
-          {
53
-            icon: lineSymbol,
54
-            offset: '100%'
55
-          }
56
-        ],
57
-        map: map
58
-      });
59
-    <% end %>
60
-  })(map);
61
-</script>
1
+<%- unless @map_marker_included_p -%>
2
+  <%- content_for :head do -%>
3
+    <%= javascript_include_tag "map_marker" %>
4
+  <%- end -%>
5
+  <%- @map_marker_included_p = true -%>
6
+<%- end -%>
7
+  map_marker(<%= map %>, {
8
+    lat: <%= location[:lat].to_json %>,
9
+    lng: <%= location[:lng].to_json %>,
10
+    radius: <%= location[:radius].to_json %>,
11
+    course: <%= location[:course].to_json %>,
12
+    speed: <%= location[:speed].to_json %>
13
+  });

+ 1 - 1
config/environments/production.rb

@@ -61,7 +61,7 @@ Huginn::Application.configure do
61 61
   end
62 62
 
63 63
   # Precompile additional assets (application.js.coffee.erb, application.css, and all non-JS/CSS are already added)
64
-  config.assets.precompile += %w( diagram.js graphing.js user_credentials.js )
64
+  config.assets.precompile += %w( diagram.js graphing.js map_marker.js user_credentials.js )
65 65
 
66 66
   # Ignore bad email addresses and do not raise email delivery errors.
67 67
   # Set this to true and configure the email server for immediate delivery to raise delivery errors.